package game;

import interfaces.impl.HumanInteraction;

import java.io.InputStream;
import java.util.ArrayList;

import antlr.collections.List;

import objects.ImageObject;
import objects.WorldObject;
import space.Space;
import universe.MessageFromClient;
import universe.Universe;
import constants.Constants;
import constants.Message;

public abstract class GameServer {

  protected Space space;

  // --------------------------------------------------------------------------------

  public GameServer() {
    space = new Space();
  }

  // --------------------------------------------------------------------------------

  public void update(double time) {
    space.update(time);
  }

  // --------------------------------------------------------------------------------

  public String getMessageUpdateWorld(int universeId) {
    String res = new String();
    res = Message.UPDATE_OBJECTS_WORLD + System.getProperty("line.separator");
    Universe universe = space.getUniverse(universeId);

    if (universe == null) {
      return res;
    }

    for (WorldObject w : universe.getListObjects()) {
      res = res + (w.messageUpdateObject());
      res = res + System.getProperty("line.separator");
    }

    return res;
  }

  // --------------------------------------------------------------------------------

  public String getMessageAgentsFromObject(String string, int idCliente, int universeId) {
    int id = Integer.parseInt(string);

    String res = Message.AGENTS_FROM_OBJECT_INVENTORY + System.getProperty("line.separator");

    System.out.println("Esta es la id del bjeto de la inventoria server: " + id);

    Universe universe = space.getUniverse(universeId);

    if (universe == null) {
      return res;
    }

    WorldObject obj = universe.getObject(id);

    if (obj == null) {
      return res;
    }

    res = res + obj.messageAgentsFromObject(idCliente);

    return res;
  }

  // --------------------------------------------------------------------------------

  public String getMessageAgentsFromObject(int objectId, int idCliente, int universeId) {
    String res = new String();
    res = Message.AGENTS_FROM_OBJECT + System.getProperty("line.separator");

    Universe universe = space.getUniverse(universeId);

    if (universe == null) {
      return res;
    }

    WorldObject obj = universe.getObject(objectId);

    if (obj != null) {
      System.out.println("Busco los agentes del objeto " + obj.getID());
      res = res + obj.messageAgentsFromObject(idCliente);
    }

    return res;
  }

  // --------------------------------------------------------------------------------

  public String getParameters(String str, int universeId) {

    String res = Message.DONT_DO_ANYTHING + System.getProperty("line.separator");

    String sd[] = str.split(";");
    int id = Integer.parseInt(sd[0]);

    String name = (sd[1]);

    System.out.println(str);

    Universe universe = space.getUniverse(universeId);

    if (universe == null) {
      return res;
    }

    WorldObject obj = universe.getObject(id);

    if (obj == null) {
      System.out.println("El objeto es nulo buscando los parametros");
      return res;
    }

    System.out.println("Me llamo:" + name + ":MM");

    for (HumanInteraction ag : obj.getListHumanInteractions()) {
      if (ag.getName().equals(name)) {
        System.out.println("tengo la interaccion con su nombre");

        if (ag.getTotalParameters() == 0) {
          ag.setActive(true);
          System.out.println("Lo puse activo, no tenia paramteros");
        } else {
          //I set the parameters and returned in the message
          res = Message.PARAMETERS_FOR_AGENT + System.getProperty("line.separator");
          res = res + ag.getParameters();
        }

        break;
      }
    }

    System.out.println("no encontre el nombre " + obj.getID());

    return res;
  }

  // --------------------------------------------------------------------------------

  public void setParameters(String str, int universeId) {
    String parameters[] = str.split("/");
    int id = Integer.parseInt(parameters[0]);
    String name = parameters[1];

    Universe universe = space.getUniverse(universeId);

    if (universe == null) {
      return;
    }

    WorldObject obj = universe.getObject(id);

    if (obj == null) {
      System.out.println("NO ENCONTRE OBJETO SERVIDORRR");
      return;
    }

    System.out.println("lo encontre");

    for (HumanInteraction ag : obj.getListHumanInteractions()) {
      if (ag.getName().equals(name)) {
        String msg = new String();
        for (int i = 2; i < parameters.length; i++) {
          msg = msg + parameters[i] + "/";
        }
        ag.setParameter(msg, universe);
        System.out.println("encontee el agente que parametrizo " + msg);
        break;
      }
    }

  }

  // --------------------------------------------------------------------------------

  public String getInventory(String string, int idClient, int universeId) {
    String msg = Message.INVENTORY_FROM_OBJECT + System.getProperty("line.separator");
    int id = Integer.parseInt(string);

    Universe universe = space.getUniverse(universeId);

    if (universe == null) {
      return msg;
    }

    WorldObject obj = universe.getObject(id);

    if (obj == null) {
      System.out.println("no hagas nada, servidor no encontre el objeto " + id);
      return Message.DONT_DO_ANYTHING + System.getProperty("line.separator");
    }

    for (WorldObject object : obj.getListObjects()) {
      if (object.getOwner() == Constants.NO_OWNER || object.getOwner() == idClient) {
        msg = msg + object.messageUpdateObject() + System.getProperty("line.separator");
      }
    }

    return msg;

  }

  // --------------------------------------------------------------------------------

  public String getBackgroundObject(String msg, int universeId) {
    Universe universe = space.getUniverse(universeId);
    if (universe == null) {
      System.out.println("NO ENCONTRE EL UNIVERSO " + universeId);
      return msg;
    }
    System.out.println("Evio el background");
    WorldObject object = universe.getBackgroundObject();
    msg = msg + object.messageUpdateObject() + System.getProperty("line.separator");
    msg = msg + universe.getSize();
    return msg;
  }

  // --------------------------------------------------------------------------------

  public ArrayList<MessageFromClient> getMessageFor(int idClient, int universeId) {
    ArrayList<MessageFromClient> listMessage = new ArrayList<MessageFromClient>();

    Universe universe = space.getUniverse(universeId);

    if (universe == null) {
      return listMessage;
    }

    listMessage = universe.getMessageFromClient(idClient);

    return listMessage;
  }

  // --------------------------------------------------------------------------------

  public abstract void load(InputStream is);

  public abstract int connectClient(int idCliente);

  public abstract void disconnectClient(int idCliente, int universeId);
  
  public abstract ArrayList<ReferenceImageObject> getAllImages();
  
  public abstract ImageObject getImageObject(int objectReference);
  
 // public abstract void load(InputStream is);
}
